import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/iconfont.css'
import axios from 'axios'
import api from './config/api.js'
import qs from 'qs'
// 导入 NProgress 包对应的JS和CSS
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import './assets/css/globle.css'
import ayn from './util/asyncWrapUtil'
import httpTemplate from './util/httpUtil'
// util
import beanUtils from './util/BeanUtils'

// ui
import GreenTitle from './components/title/GreenTitle'
import ZForm from './view/template/ZForm'
Vue.use(ayn)
Vue.prototype.$http = axios
Vue.prototype.$api = api
Vue.prototype.$qs = qs
Vue.prototype.$httpTemplate = httpTemplate
Vue.prototype.$beanUtils = beanUtils
Vue.component('GreenTitle', GreenTitle)
Vue.component('ZForm', ZForm)
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
axios.interceptors.request.use(config => {
  NProgress.start()
  config.headers.token = window.sessionStorage.getItem('token')
  // 在最后必须 return config
  return config
})

// 在 response 拦截器中，隐藏进度条 NProgress.done()
axios.interceptors.response.use(res => {
  NProgress.done()
  const data = res.data

  if (data.status === 401) {
    vm.$error(data.status + ': ' + data.message)
    if (data.data.status !== 'api-401') {
      vm.$router.push('/login')
    }
  } else if (data.status !== 200) {
    vm.$error(data.status + ': ' + data.message)
  }
  return res
}, function(err, data) {
  NProgress.done()
  if (err) {
    console.error(err)
    if (err.response) {
      console.error(err)
      console.dir(err.response)
      vm.$error(err.response.status + ':  ' + err.response.data.message)
      return
    }
    vm.$error(err.message)
  }
})

Vue.config.productionTip = false

const vm = new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
